using AutoMapper;
using BackData;
using Domain.Warehousies;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Warehouse.laneway;
using Yitter.IdGenerator;

namespace Team.API.Applocation.Hanldler.Warehouse.laneway
{
    /// <summary>
    /// 创建巷道命令处理器
    /// </summary>
    public class CreateLanewayHandler : IRequestHandler<CreateLanewayCommand, APIResult<int>>
    {
        private readonly IMapper _mapper;
        private readonly IRepository<Lanewaymodel> _lanewayRep;
        private readonly IRepository<Warehousemodel> _warehouseRep;
        private readonly ILogger<CreateLanewayHandler> _logger;

        public CreateLanewayHandler(
            IMapper mapper,
            IRepository<Lanewaymodel> lanewayRep,
            IRepository<Warehousemodel> warehouseRep,
            ILogger<CreateLanewayHandler> logger)
        {
            _mapper = mapper;
            _lanewayRep = lanewayRep;
            _warehouseRep = warehouseRep;
            _logger = logger;
        }

        public async Task<APIResult<int>> Handle(CreateLanewayCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证仓库是否存在
                var warehouse = await _warehouseRep.GetAll()
                    .Where(x => x.Id == request.Warehouseid && !x.IsDel)
                    .FirstAsync();

                if (warehouse == null)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "所属仓库不存在",
                        Result = 0
                    };
                }

                // 检查巷道编号是否重复
                var existingLaneway = await _lanewayRep.GetAll()
                    .Where(x => x.Lanewaycode == request.Lanewaycode && !x.IsDel)
                    .FirstAsync();

                if (existingLaneway != null)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = $"巷道编号 {request.Lanewaycode} 已存在",
                        Result = 0
                    };
                }

                // 映射并创建巷道
                var laneway = _mapper.Map<Lanewaymodel>(request);
                laneway.Id = YitIdHelper.NextId();

                var result = await _lanewayRep.AddAsync(laneway);

                if (result > 0)
                {
                    _logger.LogInformation($"巷道创建成功: {request.Lanewayname} ({request.Lanewaycode})");
                    return new APIResult<int>
                    {
                        Code = StateCode.OK,
                        Message = "巷道创建成功",
                        Result = result
                    };
                }
                else
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "巷道创建失败",
                        Result = 0
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "巷道创建失败");
                return new APIResult<int>
                {
                    Code = StateCode.Fail,
                    Message = $"巷道创建失败: {ex.Message}",
                    Result = 0
                };
            }
        }
    }
}
